//
//  GEMultiPickerTextField.m
//  OMS
//
//  Created by goldeney on 14/10/24.
//  Copyright (c) 2014年 ShangHai Goldeneye Techonology Co.,Ltd. All rights reserved.
//

#import "GEMultiPickerTextField.h"
#import "NSDictionary+DataFormat.h"
#import "IQUIView+IQKeyboardToolbar.h"
#import "IQUIView+Hierarchy.h"

@interface GEMultiPickerTextField ()<UIPickerViewDelegate,UIPickerViewDataSource>

@property (strong, nonatomic) UIPickerView *customPicker;
@property (strong, nonatomic) UIView *keyboardView;

@property (strong, nonatomic) NSArray *fontAry;
@property (strong, nonatomic) NSArray *defaultArray;

@property (strong, nonatomic) id target;
@property (assign, nonatomic) SEL action;

@end

@implementation GEMultiPickerTextField
@synthesize fontAry;
@synthesize defaultArray;

- (id)initWithCoder:(NSCoder *)aDecoder
{
    self = [super initWithCoder:aDecoder];
    if (self) {
        [self initWithTextField];
    }
    return self;
}

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self initWithTextField];
    }
    return self;
}

- (void)initWithTextField
{
    self.keyboardView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SuperWidth, 162)];
    self.inputView = self.keyboardView;
    
    self.customPicker = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 0, SuperWidth, 162)];
    self.customPicker.delegate = self;
    self.customPicker.dataSource = self;
    [self.keyboardView addSubview:self.customPicker];
    
//    [self addCancelDoneOnKeyboardWithTarget:self cancelAction:@selector(cancelAction:) doneAction:@selector(doneAction:) titleText:self.placeholder];
    
    [self addPreviousNextDoneOnKeyboardWithTarget:self previousAction:@selector(previousAction:) nextAction:@selector(nextAction:) doneAction:@selector(doneAction:) titleText:self.placeholder];
    
}

- (void)addDoneTarget:(id)target action:(SEL)action
{
    self.target = target;
    self.action = action;
}

-(void)doneAction:(UIBarButtonItem*)barButton
{
    //未选择任何数据设置默认值
    if (self.fontAry && self.fontAry.count>0 && !self.selectData) {
        NSMutableString *selectallData = [NSMutableString string];
        for (int i=0;i<fontAry.count;i++) {
            NSArray *value = [fontAry objectAtIndex:i];
            [selectallData appendString:[value objectAtIndex:0]];
        }
        self.selectData = selectallData;
    }
    self.text = self.selectData;
    
    if (self.target && self.action)
        [self.target performSelector:self.action withObject:nil];
    
    //关闭键盘
    [self resignFirstResponder];
}

-(void)cancelAction:(UIBarButtonItem*)barButton
{
    [self resignFirstResponder];
    
}

/*! previousAction. */
-(void)previousAction:(id)button
{
    //previousAction
    NSArray *responder =[self responderSiblings];
    NSInteger index = [responder indexOfObject:self];
    if (index-1>=0) {
        UITextField *textField = [responder objectAtIndex:index-1];
        [textField becomeFirstResponder];
        [self doneAction:nil];
    }
}

/*! nextAction. */
-(void)nextAction:(id)button
{
    //nextAction
    //[self becomeFirstResponder];
    NSArray *responder =[self responderSiblings];
    NSInteger index = [responder indexOfObject:self];
    if (index<responder.count) {
        UITextField *textField = [responder objectAtIndex:index+1];
        [textField becomeFirstResponder];
        [self doneAction:nil];
    }
}

- (void)setMultiComponents:(NSArray*)array
{
    if (array && array.count>0) {
        self.fontAry = array;
        [self.customPicker reloadAllComponents];
    }
}

- (void)setMultiComponentsDefault:(NSArray*)array
{
    if (array && array.count>0) {
        self.fontAry = array;
        [self.customPicker reloadAllComponents];
        
        //设置默认文字
        NSMutableString *selectallData = [NSMutableString string];
        for (int i=0;i<fontAry.count;i++) {
            NSArray *value = [fontAry objectAtIndex:i];
            [selectallData appendString:[value objectAtIndex:0]];
        }
        self.text = selectallData;
    }
}

- (void)setMultiComponents:(NSArray*)array defaults:(NSArray*)value
{
    if (array && array.count>0) {
        self.fontAry = array;
        self.defaultArray = value;
        [self.customPicker reloadAllComponents];
        
        //设置默认文字
        NSMutableString *selectallData = [NSMutableString string];
        for (int i=0;i<fontAry.count;i++) {
            NSArray *value = [fontAry objectAtIndex:i];
            [selectallData appendString:[value objectAtIndex:0]];
        }
        self.text = selectallData;
        
        //设置默认值
        NSMutableString *selectallValue = [NSMutableString string];
        for (int i=0;i<defaultArray.count;i++) {
            NSArray *values = [defaultArray objectAtIndex:i];
            [selectallValue appendString:[values objectAtIndex:0]];
        }
        self.selectValue = selectallValue;
    }
}

#pragma mark pickerview function

/* return cor of pickerview*/
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return [fontAry count];
}

/*return row number*/
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    NSArray *value = [fontAry objectAtIndex:component];
    return [value count];
}

/*return component row str*/
-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    NSArray *value = [fontAry objectAtIndex:component];
    NSString *string = [value objectAtIndex:row];
    return string;
}

/*choose com is component,row's function*/
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    //默认文字
    NSMutableString *selectallData = [NSMutableString string];
    for (int i=0;i<fontAry.count;i++) {
        NSArray *value = [fontAry objectAtIndex:i];
        NSInteger row = [pickerView selectedRowInComponent:i];
        [selectallData appendString:[value objectAtIndex:row]];
    }
    self.selectData = selectallData;
    
    //设置默认值
    if(!defaultArray) return;
    if(defaultArray.count<=0) return;
    NSMutableString *selectallValue = [NSMutableString string];
    for (int i=0;i<defaultArray.count;i++) {
        NSArray *values = [defaultArray objectAtIndex:i];
        [selectallValue appendString:[values objectAtIndex:row]];
    }
    self.selectValue = selectallValue;
}

@end
